#!/bin/sh 
# copyrights 
# (1)load-balance(N-WAN) Script VER 0.12 for openwrt  by feixiang@openwrt.org.cn

. /etc/functions.sh
. /lib/functions/uci-defaults.sh

nwan_debug=0

HISTORY_DIR="/etc/config/macvlan"

del_network_nwan() {
	line_num=$1
	[ "$line_num" == "1" ]&& LINE_list="100 200 300"
	[ "$line_num" == "2" ]&& LINE_list="200 300"
	[ "$line_num" == "3" ]&& LINE_list="300"
	[ "$line_num" == "4" ]&& LINE_list=" "
	for Line_NO in $LINE_list ;do
		sh ${HISTORY_DIR}/nwan_macvlan_del${Line_NO} 	
		[ "$nwan_debug" != 0 ] && echo "line 104" >> /tmp/xx_nwan_debug
		#rm -rf ${HISTORY_DIR}/macvlan_num
		#rm -rf ${HISTORY_DIR}/wan_start_idx
		rm -rf ${HISTORY_DIR}/nwan_macvlan${Line_NO}
		rm -rf ${HISTORY_DIR}/nwan_macvlan${Line_NO}_del
		#echo "rm -rf /tmp/luci*" >> ${HISTORY_DIR}/nwan_macvlan${Line_NO}
		uci delete network.wan${Line_NO}
		uci delete nwan.wan${Line_NO}
		uci delete dhcp.wan${Line_NO}
	done
}

set_network_nwan() {
	line_num=$1
	[ "$line_num" == "4" ]&& LINE_list="100 200 300"
	[ "$line_num" == "3" ]&& LINE_list="100 200"
	[ "$line_num" == "2" ]&& LINE_list="100"
	[ "$line_num" == "1" ]&& LINE_list=""
	for Line_NO in $LINE_list ;do
		uci set network.wan${Line_NO}=interface
		uci set network.wan${Line_NO}.proto=dhcp
		uci set network.wan${Line_NO}.defaultroute=0
		uci set network.wan${Line_NO}.peerdns=1
		uci set network.wan${i}.nwan=1
		uci set network.wan${i}.name=telecom
		uci set network.wan${i}.route=balance
		uci set network.wan${i}.weight=1
		echo "uci delete dhcp.wan${i}" >> ${HISTORY_DIR}/nwan_macvlan_del${Line_NO}
	done
}

set_network_3420 () {
	[ "$line_num" == "2" ] && {
		n=$line_3420
		Line_NO=100
		HISTORY_DIR="/etc/config/macvlan"
		[  -e /etc/config/macvlan ] || mkdir -p /etc/config/macvlan
		mac2=$(echo "" | awk '{srand(); printf("%X\n", rand()*90+16);}')
		sleep 1
		mac3=$(echo "" | awk '{srand(); printf("%X\n", rand()*90+16);}')
		sleep 1
		mac4=$(echo "" | awk '{srand(); printf("%X\n", rand()*90+16);}')
		sleep 1
		mac5=$(echo "" | awk '{srand(); printf("%X\n", rand()*90+16);}')
		sleep 1
		#mac6=$(echo "" | awk '{srand(); printf("%X\n", rand()*90+16);}')
		#sleep 1
		#mac=$(echo 00:${mac2}:${mac3}:${mac4}:${mac5}:01)
		echo "" > ${HISTORY_DIR}/nwan_macvlan${Line_NO}
		echo "" > ${HISTORY_DIR}/nwan_macvlan_del${Line_NO}
		i=100
		n=$((n+100))

		while [ "$i" -lt "$n" ]
		do
			echo "ifdown wan"$i" &" >> ${HISTORY_DIR}/nwan_macvlan_del${Line_NO}
			#生成不同的mac

			mac6=$(printf "%x" ${i})
			mac6=$(echo ${mac6:0-1:2})
			mac=$(echo 00:${mac2}:${mac3}:${mac4}:${mac5}:${mac6})

			# add /lib/nwan/nwan_macvlan

			echo "ip link add link br-lan vth"$i" type macvlan" >> ${HISTORY_DIR}/nwan_macvlan${Line_NO}
			echo "ifconfig vth"$i" hw ether $mac" >> ${HISTORY_DIR}/nwan_macvlan${Line_NO}
			echo "ifconfig vth"$i" up" >> ${HISTORY_DIR}/nwan_macvlan${Line_NO}
			echo "ip link delete vth"$i" type macvlan" >> ${HISTORY_DIR}/nwan_macvlan_del${Line_NO}

			# add /etc/config/network
			uci set network.wan${i}=interface
			uci set network.wan${i}.ifname=vth${i}
			uci set network.wan${i}.proto=pppoe
			uci set network.wan${i}.username=$pppoe_name
			uci set network.wan${i}.password=$pppoe_pw
			uci set network.wan${i}.defaultroute=0
			uci set network.wan${i}.peerdns=1
			uci set network.wan${i}.nwan=1 
			uci set network.wan${i}.name=telecom 
			uci set network.wan${i}.route=balance 
			uci set network.wan${i}.weight=1 

			echo "uci delete network.wan${i}" >> ${HISTORY_DIR}/nwan_macvlan_del${Line_NO}
		
		
			# add /etc/config/dhcp

			uci set dhcp.vth${i}=dhcp 
			uci set dhcp.vth${i}.interface=wan${i}
			uci set dhcp.vth${i}.ignore=1 
			echo "uci delete dhcp.vth${i}" >> ${HISTORY_DIR}/nwan_macvlan_del${Line_NO}	
			echo "wan${i} 配置已经完成"
			i=$((i+1))
		done
	}
}


del_network_switch() {

	uci delete network.@switch_vlan[-1]
	uci delete network.@switch_vlan[-1]
	uci delete network.@switch_vlan[-1]
	uci delete network.@switch_vlan[-1]
	uci delete network.@switch_vlan[-1]
}


get_switch_numset() {

	local enable
	local macvlan_num
	local line_num 
	local route_model
	config_get enable $1 enable 0
	echo "line 154 1=$enable 2=$line_num 3=$macvlan_num $route_model" >> /tmp/n1041 

	[ "$enabled" == "0" ] && return 0

	config_get line_num $1 line_num 1 
	config_get macvlan_num $1 macvlan_num

	route_model=$(awk 'BEGIN{FS="[ \t]+:[ \t]"} /machine/ {print $2}' /proc/cpuinfo)

	echo "line 296 1=$enable 2=$line_num 3=$macvlan_num $route_model" >> /tmp/n1041 

case $route_model in
	*TL-MR3420)
		#71xx 3420 841-v7 300r.. 
		del_network_nwan $line_num
		set_network_3420 $line_num
	;;

	*"TL-WR1041N v2" | *"TL-WR841ND v8")
		if_idx=eth0
		del_network_nwan $line_num $if_idx
		set_network_nwan $line_num $if_idx
		del_network_switch

		case $line_num in
			1) 
			ucidef_add_switch_vlan "eth0" "1" "0t 2 3 4 5"
			ucidef_add_switch_vlan "eth0" "2" "0t 1"
			uci set network.wan.ifname="eth0.2"
			ifup wan&
			;;
			2) 
			ucidef_add_switch_vlan "eth0" "1" "0t 3 4 5"
			ucidef_add_switch_vlan "eth0" "3" "0t 2"
			ucidef_add_switch_vlan "eth0" "2" "0t 1"
			uci set network.wan.ifname="eth0.2"
			uci set network.wan100.ifname="eth0.3"
			ifup wan&ifup wan100&
			;;
			3)
			ucidef_add_switch_vlan "eth0" "1" "0t 4 5"
			ucidef_add_switch_vlan "eth0" "4" "0t 3"
			ucidef_add_switch_vlan "eth0" "3" "0t 2"
			ucidef_add_switch_vlan "eth0" "2" "0t 1"
			uci set network.wan.ifname="eth0.2"
			uci set network.wan100.ifname="eth0.3"
			uci set network.wan200.ifname="eth0.4"
			ifup wan & ifup wan100 & ifup wan200 &
			;;
			4) 
			ucidef_add_switch_vlan "eth0" "1" "0t 5"
			ucidef_add_switch_vlan "eth0" "5" "0t 4"
			ucidef_add_switch_vlan "eth0" "4" "0t 3"
			ucidef_add_switch_vlan "eth0" "3" "0t 2"
			ucidef_add_switch_vlan "eth0" "2" "0t 1"
			uci set network.wan.ifname="eth0.2"
			uci set network.wan100.ifname="eth0.3"
			uci set network.wan200.ifname="eth0.4"
			uci set network.wan300.ifname="eth0.5"
			ifup wan&ifup wan100&ifup wan200&ifup wan300&
			;;
		esac 
	;;


	*TL-WR941ND)
		#srart  71xx-TP941N 841N V3..          
		del_network_nwan 
		set_network_nwan  
		uci set network.lan.ifname="lan1 lan2 lan3 lan4" 
		uci set network.wan.ifname=wan 
			case $line_num in
				1)
				#line 1  复位和删除多余的wan口数据
				ifup wan&
				;;
				2)
				#line 2 lan4 wan 100
				uci set network.lan.ifname="lan1 lan2 lan3"
				uci set network.wan100.ifname=lan4 
				uci set dhcp.lan4=dhcp 
				uci set dhcp.lan4.interface=wan100
				uci set dhcp.lan4.ignore=1 
				ifup wan&ifup wan100&
				;;
				3)
				#line 3 lan4 wan100 lan3 wan200
				uci set network.lan.ifname="lan1 lan2"
				uci set network.wan100.ifname=lan4
				uci set network.wan200.ifname=lan3
				uci set dhcp.lan4=dhcp
				uci set dhcp.lan4.interface=wan100
				uci set dhcp.lan4.ignore=1
				uci set dhcp.lan3=dhcp
				uci set dhcp.lan3.interface=wan200
				uci set dhcp.lan3.ignore=1
				ifup wan&ifup wan100&ifup wan200&
				;;
				3)
				#line 4 lan4 wan100 lan3 wan200 lan2 wan300
				uci set network.lan.ifname=lan1
				uci set network.wan100.ifname=lan4
				uci set network.wan200.ifname=lan3
				uci set network.wan300.ifname=lan2
				uci set dhcp.lan4=dhcp 
				uci set dhcp.lan4.interface=wan100
				uci set dhcp.lan4.ignore=1 
				uci set dhcp.lan3=dhcp 
				uci set dhcp.lan3.interface=wan200
				uci set dhcp.lan3.ignore=1 
				uci set dhcp.lan2=dhcp 
				uci set dhcp.lan2.interface=wan300
				uci set dhcp.lan2.ignore=1		
				ifup wan&ifup wan100&ifup wan200&ifup wan300&
				;;
			esac
	;; 
	# 941v2 841v3 end
esac


}

	config_load nwannumset
	config_foreach get_switch_numset switch_numset
